//
//  BookListTableViewController.m
//  XMLY
//
//  Created by 曾杰 on 15/11/4.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "BookListTableViewController.h"
#import "PKRequestManager.h"
#import "SoundBookModel.h"
#import "SoundBookCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "SoundPlayListController.h"
#import "AlertView.h"
@interface BookListTableViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *dataArr;//数据数组
@end

@implementation BookListTableViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

//配置数据
- (void)configureData
{
    [PKRequestManager requestWithType:GET urlString:_urlStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves) error:nil];
        NSArray *arr = dic[@"list"];
        for (NSDictionary *listDic in arr) {
            SoundBookModel *model = [[SoundBookModel alloc]init];
            [model setValuesForKeysWithDictionary:listDic];
            [self.dataArr addObject:model];
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureData];
    [self.tableView registerClass:[SoundBookCell class] forCellReuseIdentifier:@"soundBookCell"];
    self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-100);
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        [self configureData];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self configureData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 0;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SoundBookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"soundBookCell" forIndexPath:indexPath];
    SoundBookModel *model = self.dataArr[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
    cell.titleLabel.text = model.title;
    cell.introLabel.text = model.intro;
    float playsCount = model.playsCounts / 10000.0;
    cell.playCountLabel.text = [[NSString stringWithFormat:@"%.1f",playsCount] stringByAppendingString:@"万"];
    cell.tracksCounts.text = [[NSString stringWithFormat:@"%ld",model.tracksCounts] stringByAppendingString:@"集"];
    
    return cell;
}
#pragma mark - UITableView Delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}



@end
